<!DOCTYPE html>
<html>

{include file='public/header'}
<style>
    .layui-layer-page .layui-layer-content{overflow: visible;}
</style>
<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="javascript:void(0)">首页</a>
            <a href="javascript:void(0)">账号管理</a>
            <a><cite>小程序审核管理</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" id="reload" title="刷新">
            <i class="layui-icon" style="line-height:30px"><i class="iconfont">&#xe6aa;</i></i>
        </a>
    </div>
    <div class="x-body">
        <div class="layui-tab" lay-filter="tabs">
            <ul class="layui-tab-title">
                <li class="layui-this">草稿箱</li>
                <li>模板库</li>
                <!-- <li>标准模板库</li> -->
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table id="getTemplatedRaft" lay-filter="getTemplatedRaft"></table>
                </div>
                <div class="layui-tab-item">
                    <table id="getTemplate1" lay-filter="getTemplate1"></table>
                </div>
                <div class="layui-tab-item">
                    <table id="getTemplate2" lay-filter="getTemplate2"></table>
                </div>
            </div>
        </div>

        <form class="layui-form layui-row" style="margin-top:15px;margin-bottom: 10px;">
            <div class="layui-inline">
                <label class="layui-form-label w-auto">公司主体：</label>
                    <div class="layui-input-block">
                    <select name="company_entity_id">
                        <option value="">请选择</option>
                        <option value="1">比翼鸟</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label w-auto">小程序名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="app_name" placeholder="请输入小程序名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label w-auto">小程序ID：</label>
                <div class="layui-input-inline">
                    <input type="text" name="app_id" placeholder="请输入小程序ID" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" style="margin-left:10px">
                <button class="layui-btn" lay-submit lay-filter="searchListAuthorizersDetails">搜索</button>
                <button type="reset" id="resetList" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
        <table id="getAuthorizersDetails" lay-filter="getAuthorizersDetails"></table>
    </div>
    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加小程序授权</button>
            <button class="layui-btn layui-btn-sm" lay-event="configuration">配置域名</button>
            <button class="layui-btn layui-btn-sm" lay-event="privacy">设置用户隐私保护指引</button>
            <button class="layui-btn layui-btn-sm" lay-event="version">设置基础库</button>
            <button class="layui-btn layui-btn-sm" lay-event="pub_template">设置订阅消息</button>
            <button class="layui-btn layui-btn-sm" lay-event="batch_setting">批量小程序操作</button>
        </div>
    </script>
    <script type="text/html" id="tbBar">
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="message">基础信息</a>
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="template_message">模板信息</a>
        <button class="layui-btn layui-btn-xs" lay-event="more">
            更多
            <i class="layui-icon layui-icon-down layui-font-12"></i>
        </button>
    </script>
    <script type="text/html" id="tbBarRaft">
        <a class="layui-btn layui-btn-xs" lay-event="update1">上传模板</a>
        <!-- <a class="layui-btn layui-btn-xs" lay-event="update2">上传标准模板</a> -->
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
    </script>
    <!-- 普通模板库 -->
    <script type="text/html" id="tbBarCommonRaft">
        <a class="layui-btn layui-btn-xs" lay-event="submit">提交体验版</a>
        <a class="layui-btn layui-btn-xs" lay-event="postSubmit">提交审核</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
    </script>
    <!-- 标准模板库 -->
    <!-- <script type="text/html" id="tbBarStandardRaft">
        <a class="layui-btn layui-btn-xs" lay-event="submit">提交体验版</a>
        <a class="layui-btn layui-btn-xs" lay-event="postSubmit">提交审核</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
    </script> -->
    <script>
        // 获取已授权的授权方列表
        let params = {
            elem: '#getAuthorizersDetails'
            , toolbar: '#toolbar'
            , url: '/admin/OpenWeixin/getAuthorizersDetails' //数据接口
            , id: 'getAuthorizersDetails'
            , limit:10
            , page:true
            , cols: [[ //表头
                { field: 'app_name', title: '小程序名称', align:'center'}
                , { field: 'app_id', title: '小程序appid', align:'center'}
                , {title: '公司主体', templet: function (d) {
                        let company_name = ''
                        if (d.company_entity_id == 1) {
                            company_name = '比翼鸟';
                        }
                        return company_name
                    }, align:'center'
                }
                , { field: 'status',title:'审核状态', templet: function(d){
                        Ajax('/admin/OpenWeixin/getLatestAuditStatus',{app_id:d.app_id},'get',false).then(res=>{
                            let status = ['审核成功','审核被拒绝','审核中','已撤回','审核延后'][res.data.status] ?? ''
                            $('td[data-field="status"]').eq(d.LAY_TABLE_INDEX).find('.layui-table-cell').html(status); 
                            $('td[data-field="audit_auditid"]').eq(d.LAY_TABLE_INDEX).find('.layui-table-cell').html(res.data.auditid); 
                        })
                        return ''
                    }, align:'center'
                }
                , { field: 'audit_auditid',title:'审核ID', align:'center'}
                , { toolbar: '#tbBar', title: '操作', fixed:'right', width: 240, align:'center' }
            ]]
            , parseData:function (res) { //res 即为原始返回的数据
                if(res.code == 200){
                    return {
                        "data": res.data.data, //解析数据列表
                        "code": res.code, //解析接口状态
                        'count':res.data.total_count//解析数据长度
                    };
                }else{
                    layer.msg(res.message,{icon:5,time:1000});
                    return {
                        "data": [], //解析数据列表
                        "code": 200, //解析接口状态
                        'count':0//解析数据长度
                    };
                }
            },
        }
        tableList(params)

        // 获取草稿箱列表
        let raftParams = {
            elem: '#getTemplatedRaft'
            , toolbar: false
            , url: '/admin/OpenWeixin/getTemplatedRaft' //数据接口
            , id: 'getTemplatedRaft'
            , page:false
            , cols: [[ //表头
                { field: 'draft_id', title: '草稿ID',width:100 , align:'center'}
                , { field: 'user_version', title: '版本号',width:100 , align:'center'}
                , { field: 'user_desc', title: '描述', width:500,align:'center'}
                , { field: 'source_miniprogram', title: '来源小程序', align:'center'}
                , { field: 'source_miniprogram_appid', title: '来源小程序appid', align:'center'}
                , { field: 'developer', title: '最近上传开发者', align:'center'}
                , { field: 'create_time', title: '最近提交时间', align:'center' , templet: function (d) {
                        return dayjsFormat(d.create_time)
                    } 
                }
                , { toolbar: '#tbBarRaft', title: '操作',fixed:'right', width: 280, align:'center' }
            ]]
        }
        tableList(raftParams)

        // 获取模板列表
        let getTemplateParams = {
            elem: '#getTemplate1'
            , toolbar: false
            , url: '/admin/OpenWeixin/getTemplate' //数据接口
            , id: 'getTemplate1'
            , page: false
            , cols: [[ //表头
                { field: 'user_version', title: '版本号',width:100 , align:'center'}
                , { field: 'user_desc', title: '描述', width:700,align:'center'}
                , { field: 'template_id', title: '模板 id', align:'center'}
                , { field: 'draft_id', title: '草稿 id', align:'center'}
                , { field: 'create_time', title: '上传时间', width:200, align:'center' , templet: function (d) {
                        return dayjsFormat(d.create_time)
                    } 
                }
                , { field: 'audit_status', title: '审核状态', align:'center' , templet: function (d) {
                        if (d.audit_status == 0) {
                            return '未提审核';
                        } else if (d.audit_status == 1) {
                            return '审核中';
                        }else if (d.audit_status == 2) {
                            return '审核驳回';
                        }else if(d.audit_status == 3) {
                            return '审核通过';
                        }else if(d.audit_status == 4){
                            return '提审中'
                        }else if(d.audit_status == 5){
                            return '提审失败'
                        }else{
                            return ''
                        }
                    } 
                }
                , { toolbar: '#tbBarRaft', title: '操作', width: 300, align:'center' }
            ]]
        }
        

        layui.use(['table','element','dropdown'], function () {
            var table = layui.table;
            var element = layui.element;
            var dropdown = layui.dropdown;
            var form = layui.form;
            // 绑定的小程序
            table.on('toolbar(getAuthorizersDetails)', function (obj) {
                var event = obj.event;
                if (event === 'add') {
                    Ajax('/admin/OpenWeixin/getPreAuthorizationUrl', {},'get').then(result => {
                        layer.open({
                            type: 2,
                            area: ['750px','700px'],
                            fix: false, //不固定
                            maxmin: true,
                            shadeClose: true,
                            shade:0.4,
                            title: '添加小程序授权',
                            content: result.data.url,
                            btn:null,
                            cancel: function(index, layero){ 
                                table.reloadData('getAuthorizersDetails')
                                layer.close(index)
                                return false; 
                            }
                        });
                    });
                } else if(event === 'configuration'){
                    x_admin_show('配置小程序服务器域名', './seriver_domain.html', 800, 700);
                } else if(event === 'privacy'){
                    x_admin_show('设置用户隐私保护指引', './privacy.html', 800, 700);
                } else if(event === 'version'){
                    x_admin_show('设置最低基础库版本', './version.html', 500, 500); 
                } else if(event === 'pub_template'){
                    x_admin_show('设置订阅消息模板', './pub_template.html', 800, 600); 
                } else if(event === 'batch_setting'){
                    x_admin_show('批量小程序操作', './batch_setting.html', 800, 600); 
                }
            });
            table.on('tool(getAuthorizersDetails)', function (obj) {
                var event = obj.event;
                if(event === 'message'){
                    x_admin_show('查看小程序信息', './info.html?app_id='+obj.data.app_id, 1000, 700,null);
                } else if(event === 'template_message'){
                    x_admin_show('查看小程序订阅消息模板', './template_message.html?app_id='+obj.data.app_id, 1000, 700,null);
                } else if(event === 'more'){
                    let data = [{
                        title:'发布',
                        id:'postRelease'
                    },{
                        title:'版本回退',
                        id:'postRevertCodeRelease'
                    },{
                        title:'加急审核',
                        id:'postSpeedupCodeAudit'
                    },{
                        title:'撤回审核',
                        id:'postUndoAudit'
                    },{
                        title:'添加体验者',
                        id:'addTester'
                    },{
                        title:'移除体验者',
                        id:'postUnBindTester'
                    }];
                    dropdown.render({
                        elem: this //触发事件的 DOM 对象
                        ,show: true //外部事件触发即显示
                        ,data 
                        ,click: function(menudata){
                            if(menudata.id === 'postRelease'){
                                AnyAjax('确定要发布吗？','/admin/OpenWeixin/postRelease',{app_id:obj.data.app_id},'getAuthorizersDetails')
                            } else if(menudata.id === 'postRevertCodeRelease'){
                                AnyAjax('确定要版本回退吗？','/admin/OpenWeixin/postRevertCodeRelease',{app_id:obj.data.app_id},'getAuthorizersDetails')
                            } else if(menudata.id === 'postSpeedupCodeAudit'){
                                AnyAjax('确定要加急审核吗？','/admin/OpenWeixin/postSpeedupCodeAudit',{app_id:obj.data.app_id,auditid:obj.data.auditid},'getAuthorizersDetails')
                            } else if(menudata.id === 'postUndoAudit'){
                                AnyAjax('确定要撤回审核吗？','/admin/OpenWeixin/postUndoAudit',{app_id:obj.data.app_id},'getAuthorizersDetails')
                            } else if(menudata.id === 'addTester' || menudata.id === 'postUnBindTester'){
                                let tip = menudata.id === 'addTester' ? '添加体验者' : '移除体验者';
                                let url = menudata.id === 'addTester' ? '/admin/OpenWeixin/postBindTester' : '/admin/OpenWeixin/postUnBindTester'
                                layer.prompt({title: tip, formType: 0}, function(pass, index){
                                    Ajax(url,{
                                        app_id:obj.data.app_id,
                                        wechat_id:pass
                                    },'post').then(res => {
                                        if(res.code == 200){
                                            layer.msg('操作成功', {icon: 1},function () {
                                                layer.close(index);
                                            });
                                        }else{
                                            layer.close(index);
                                        }
                                    })
                                });
                            }
                        }
                        ,align: 'right' //右对齐弹出
                        ,style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                    })
                }
            });
            // 小程序模板库
            table.on('tool(getTemplatedRaft)', function (obj) {
                var event = obj.event;
                var draft_id = obj.data.draft_id
                if(event === 'update1'){
                    AnyAjax('确定要添加至普通模板库吗？','/admin/OpenWeixin/postTotemplate',{draft_id,template_type:0},'getTemplatedRaft')
                } else if(event === 'update2'){
                    AnyAjax('确定要添加至标准模板库吗？','/admin/OpenWeixin/postTotemplate',{draft_id,template_type:1},'getTemplatedRaft')
                } else if(event === 'delete'){
                    deleteTemplate(obj.data.template_id)
                }
            });
            // 小程序模板库
            table.on('tool(getTemplate1)', function (obj) {
                getTemplateFn(obj,1)
            });
            table.on('tool(getTemplate2)', function (obj) {
                getTemplateFn(obj,2)
            });

            // tab切换
            element.on('tab(tabs)', function(ele){
                let eleIndex = ele.index;
                if(eleIndex == 1 || eleIndex == 2){
                    if(eleIndex == 1){
                        getTemplateParams.cols[0][getTemplateParams.cols[0].length - 1].toolbar = '#tbBarCommonRaft'
                        getTemplateParams.elem = '#getTemplate1'
                        getTemplateParams.id = 'getTemplate1'
                    }else{
                        getTemplateParams.elem = '#getTemplate2'
                        getTemplateParams.id = 'getTemplate2'
                        getTemplateParams.cols[0][getTemplateParams.cols[0].length - 1].toolbar = '#tbBarStandardRaft'
                    }
                    getTemplateParams.url = '/admin/OpenWeixin/getTemplate?template_type='+ Number(eleIndex-1)
                    tableList(getTemplateParams)
                }else{
                    getTemplateParams.cols[0][getTemplateParams.cols[0].length - 1].toolbar = '#tbBarRaft'
                    tableList(getTemplateParams)
                }
                
            });

            form.on('submit(searchListAuthorizersDetails)', function(data){
                params.where = data.field
                tableList(params)
                return false
            })
        });

        // 删除代码模板
        function deleteTemplate(template_id,table_id){
            DeleteAjax('/admin/OpenWeixin/deleteTemplate',{template_id:template_id},table_id)
        }
        // 普通模板、标准模板 方法
        function getTemplateFn(obj,type){
            var event = obj.event;
            var {draft_id,source_miniprogram_appid,user_version,user_desc,template_id} = obj.data
            if(event === 'delete'){
                deleteTemplate(obj.data.template_id,'getTemplate'+type)
            } else if(event === 'submit'){
                chooseSelect('submit',obj.data)
            } else if(event === 'postSubmit'){
                chooseSelect('postSubmit',obj.data)
            } 
        }

        // 上传代码并生成体验版
        function postCommit(option,id,mask){
            AnyAjax('确认提交体验版吗？','/admin/OpenWeixin/postCommit',option,id,mask)
        }
        // 提交代码审核
        function postSubmitAudit(option,id,mask){
            AnyAjax('确认提交审核吗？','/admin/OpenWeixin/postSubmitAudit',option,id,mask)
        }


        let xmSelectGetAuthorizersOption = {
            initValue:'',//初始值
            el:'#GetAuthorizers',
            url:'/admin/OpenWeixin/getAuthorizersSelect',
            pageSize:5,
            direction:'down',//下拉方向
            isData:true,
            prop:{
                name:'app_name',
                value:"app_id"
            },
            radio:true,
            clickClose:true,
            label: {
                type: 'text'
            },
            name:'extAppid',
        }
        // 选择小程序
        function chooseSelect(type,option){
            let {draft_id,source_miniprogram_appid,user_version,user_desc,template_id} = option;
            let content = `
                    <div class="x-body chooseVideo" style='width:500px;height:300px'>
                        <form class="layui-form" lay-filter="chooseApplet">
                            <div class="layui-form-item">
                                <label class="layui-form-label">提交类型：</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="submit_type" lay-filter="submit_type" value="one" title="单个" checked>
                                    <input type="radio" name="submit_type" lay-filter="submit_type" value="batch" title="批量">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">公司主体：</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="company_entity_id" lay-filter="company_entity_id" value="" title="全部" checked>
                                    <input type="radio" name="company_entity_id" lay-filter="company_entity_id" value="1" title="比翼鸟">
                                </div>
                            </div>
                            <div class="layui-form-item" style='display: flex;'>
                                <label class="layui-form-label">
                                    <span class="x-red">*</span>选择小程序
                                </label>
                                <div id="GetAuthorizers" style="width: 100%;flex: 1;"></div>
                            </div>
                            <button id="submitTemplate" class="layui-btn" lay-submit lay-filter="submitTemplate" style="display: none;" />
                        </form>
                    </div>
                `;
            layer.open({
                type: 1,
                title: '选择小程序',
                btn: ['确定', '取消'],
                maxHeight: '800',
                content,
                success:function(){
                    xmSelectGetAuthorizersOption.url = '/admin/OpenWeixin/getAuthorizersSelect'
                    xmSelectGetAuthorizersOption.initValue = ''
                    xmSelectGetAuthorizersOption.radio = true;
                    xmSelectGetAuthorizersOption.clickClose = true;
                    xmSelectGetAuthorizersOption.label = {
                        type:'text'
                    };
                    xmSelectA(xmSelectGetAuthorizersOption)
                    form.render('radio');
                    form.on('radio(submit_type)', function(data){
                        let app_id = form.val("chooseApplet").extAppid.split(',');
                        if(data.value == 'one'){
                            xmSelectGetAuthorizersOption.initValue = app_id.length == 0 ? '' : [app_id[0]]
                            xmSelectGetAuthorizersOption.radio = true;
                            xmSelectGetAuthorizersOption.clickClose = true;
                            xmSelectGetAuthorizersOption.label = {
                                type:'text'
                            };
                        }else{
                            xmSelectGetAuthorizersOption.initValue = app_id ?? ''
                            xmSelectGetAuthorizersOption.radio = false;
                            xmSelectGetAuthorizersOption.clickClose = false;
                            xmSelectGetAuthorizersOption.label = {
                                type:'block',
                                block: {
                                    showCount:8,
                                }
                            };
                        }
                        xmSelectA(xmSelectGetAuthorizersOption)
                    });
                    form.on('radio(company_entity_id)', function(data){
                        xmSelectGetAuthorizersOption.url = '/admin/OpenWeixin/getAuthorizersSelect?company_entity_id='+data.value
                        xmSelectA(xmSelectGetAuthorizersOption)
                    });
                },
                yes: function(layMask){
                    let data = form.val("chooseApplet")
                    // 上传代码并生成体验版
                    if(type == 'submit'){
                        let datas = {
                            template_id,
                            app_id:data.extAppid,
                            user_version,
                            user_desc,
                            ext_json:{
                                extAppid:"wxf1e0e710b3243dce",
                                window: {
                                    "backgroundTextStyle": "light",
                                    "navigationBarBackgroundColor": "#fff",
                                    "navigationBarTitleText": "",
                                    "navigationBarTextStyle": "black"
                                },
                            }
                        }
                        if(form.val("chooseApplet").submit_type == 'one'){
                            postCommit(datas,'getTemplate'+type,layMask)
                        }else{
                            batchTask({
                                app_ids:form.val("chooseApplet").extAppid.split(','),
                                task_method:'postCommit',
                                task_data:datas
                            },layMask)
                        }
                    }
                    // 提交审核
                    if(type == 'postSubmit'){
                        let data = form.val("chooseApplet")
                        Ajax('/admin/OpenWeixin/getAllCategoryName', { app_id:data.extAppid.split(',')[0] },'get').then(result => {
                            return result
                        }).then(val=>{
                            if(type == 'postSubmit'){
                                let datas = {
                                    app_id:data.extAppid,
                                    item_list:val.data,
                                }
                                if(form.val("chooseApplet").submit_type == 'one'){
                                    postSubmitAudit(datas,'getAuthorizersDetails',layMask)
                                }else{
                                    batchTask({
                                        app_ids:form.val("chooseApplet").extAppid.split(','),
                                        task_method:'postSubmitAudit',
                                        task_data:datas
                                    },layMask)
                                }
                            }
                        });
                    }
                }
            });
        }
    </script>
</body>

</html>